package com.example.apple.paopo.fragment;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.apple.paopo.R;
import com.example.apple.paopo.activity.MainActivity;
import com.example.apple.paopo.activity.MessageEditorActivity;
import com.example.apple.paopo.adapter.mFragmentPagerAdapter;
import com.example.apple.paopo.base.BaseFragment;
import com.example.apple.paopo.fragment.world.FocusOnFragment;
import com.example.apple.paopo.fragment.world.FoundFragment;
import com.example.apple.paopo.fragment.world.RecommendedFragment;
import com.vondear.rxtools.view.RxToast;
import com.vondear.rxtools.view.dialog.RxDialogChooseImage;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.vondear.rxtools.view.dialog.RxDialogChooseImage.LayoutType.TITLE;

/**
 * Created by apple on 17/12/9.
 */
//世界消息
@ContentView(R.layout.fragment_worldmessage)
public class WorldMessageFragment extends BaseFragment{



    private static final String[] CHANNELS = new String[]{"发 现","推 荐  ", "关 注"};
    private List<String> mDataList;
    private List<Fragment> framgents;

    @ViewInject(R.id.magic_indicator)
    MagicIndicator magicIndicator;

    @ViewInject(R.id.worldmessage_viewpager)
    private ViewPager mViewPager;

    @ViewInject(R.id.tool_title)
    TextView toolText;

    @ViewInject(R.id.tool_break)
    ImageView breakImage;

//    @ViewInject(R.id.world_sendbutton)
//    Button world_sendbutton;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       View view= x.view().inject(this,inflater,container);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mDataList = Arrays.asList(CHANNELS);
        framgents =new ArrayList<Fragment>();
        initFramgents();
        mViewPager.setAdapter (new mFragmentPagerAdapter(getChildFragmentManager(),framgents));
        initMagicIndicator1();
        toolText.setText("");
        breakImage.setVisibility(View.GONE);
    }

    @Event(R.id.world_sendbutton)
    private void sendButtonAction(View view){
        startActivity(new Intent(getActivity(),MessageEditorActivity.class));
    }
    @Event(R.id.tool_more)
     private void moreAction(View view){
        RxToast.success("sdfsdf");
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    private void initMagicIndicator1() {

        CommonNavigator commonNavigator = new CommonNavigator(getContext());
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDataList == null ? 0 : mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(mDataList.get(index));
                simplePagerTitleView.setNormalColor(Color.parseColor("#88ffffff"));
                simplePagerTitleView.setSelectedColor(Color.WHITE);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mViewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setColors(Color.parseColor("#00BCD4"));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        LinearLayout titleContainer = commonNavigator.getTitleContainer(); // must after setNavigator
        titleContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        titleContainer.setDividerPadding(UIUtil.dip2px(getContext(), 15));
        titleContainer.setDividerDrawable(getResources().getDrawable(R.drawable.simple_splitter));
        ViewPagerHelper.bind(magicIndicator, mViewPager);
    }


    private void initFramgents() {
            framgents.add(new FoundFragment());
            framgents.add(new RecommendedFragment());
            framgents.add(new FocusOnFragment());

    }


}
